﻿#include "CBlockLegacy.h"

#include "../Utils/Logger.h"
#include "../Utils/Utils.h"

C_Block* C_BlockSource::getBlock(const vec3_ti& block) const {
	using getBlock_t = C_Block*(__fastcall*)(const C_BlockSource*, const vec3_ti&);
	static getBlock_t getBlock = reinterpret_cast<getBlock_t>(Utils::Utils::getBase() + 36661680);
	return getBlock(this, block);
}

C_BlockActor* C_BlockSource::getBlockEntity(const vec3_ti& block) const {
	using getBlockEntity_t = C_BlockActor*(__fastcall*)(const C_BlockSource*, const vec3_ti&);
	static getBlockEntity_t getBlockEntity = reinterpret_cast<getBlockEntity_t>(Utils::Utils::getBase() + 36668896);
	return getBlockEntity(this, block);
}

void C_BlockSource::rayTraceBlocks(vec3_t& start, vec3_t& end, void* ent) {
	using rayTrace_t = RayTraceResult*(__fastcall*)(const C_BlockSource* _this, RayTraceResult* hitres, vec3_t& start, vec3_t& end, bool idk1, bool idk2, int idkInt, bool idk3, bool idk4, void* ent);
	static rayTrace_t rayTrace = reinterpret_cast<rayTrace_t>(Utils::Utils::getBase() + 36686336);

	RayTraceResult res;
	rayTrace(this, &res, start, end, false, false, 200, true, false, ent);
}

C_Block* C_BlockSource::getLiquidBlock(const vec3_ti& block) const {
	using getLiquidBlock_t = C_Block*(__fastcall*)(const C_BlockSource*, const vec3_ti&);  // E8 41 7D 6A 01 48 8B C8 48 8B 46 10
	static getLiquidBlock_t getLiquidBlock = Utils::Utils::FuncFromSigOffset<getLiquidBlock_t>(Utils::Utils::getBase() + 36707073, 1);
	return getLiquidBlock(this, block);  // E8 41 7D 6A ?? ?? ?? ?? ?? ?? ?? ??
}

/*
float C_BlockSource::getBrightness(const vec3_ti& block) const  {
	using getBrightness_t = float (__fastcall*)(const C_BlockSource*, const vec3_ti&);  //E8 ? ? ? ? F3 0F 5C 05 ? ? ? ? EB 9A
	static getBrightness_t getBrightness = reinterpret_cast<getBrightness_t>(Utils::Utils::getBase() + 36707073);
	return getBrightness(this, block);
}
*/

bool C_BlockLegacy::getCollisionShape(AABB& collShapeOut, C_Block& block, C_BlockSource& blockSource, const vec3_ti& pos) {
	return Utils::Utils::CallVFunc<5, bool, AABB*, C_Block*, C_BlockSource*, const vec3_ti*, C_Entity*>(this, &collShapeOut, &block, &blockSource, &pos, nullptr);
}

void C_BlockLegacy::liquidGetFlow(vec3_t& flowOut, C_BlockSource& reg, const vec3_ti& pos) const {
	using liquid_getFlow_t = int(__fastcall*)(const C_BlockLegacy*, vec3_t*, C_BlockSource*, const vec3_ti*);
	static liquid_getFlow_t liquid_getDepth = Utils::Utils::FuncFromSigOffset<liquid_getFlow_t>(Utils::Utils::getBase() + 41282831, 1);

	liquid_getDepth(this, &flowOut, &reg, &pos);
}

bool C_BlockLegacy::hasWater(C_BlockSource& reg, const vec3_ti& pos) const {
	if (this->material->isLiquid) {
		return !this->material->isSuperHot;
	}
	const auto liquidBlock = reg.getLiquidBlock(pos)->toLegacy();
	const auto material = liquidBlock->material;
	return this != liquidBlock && material->isLiquid && !material->isSuperHot;
}

void C_BlockLegacy::setExplodeable(float a2) {
	using setExplodeable = void(__fastcall*)(C_BlockLegacy*, float);
	static setExplodeable explode = reinterpret_cast<setExplodeable>(Utils::Utils::getBase() + 39705968);
	explode(this, a2);
}
